<?php
namespace App\Common\Lib\Utils;

use App\Common as com;
use App\Common\Lib\Utils\ArrayHelper as nameArrayHelper;
use App\Common\Lib\Utils as nameUtils;

class ArrayHelper extends com\Lib\LibBase {

	/**
	 * @var array
	 */
	protected array $data;

	/**
	 * @var bool
	 */
	private bool $unique = false;

	/**
	 * @var int|null
	 */
	private ?int $sort = null;

	/**
	 * @var string|null
	 */
	private ?string $setType = null;

	/**
	 * DataCollection constructor.
	 * @param array $data
	 */
	public function __construct( array $data ) {
		$this->data = $data;
		//
		parent::__construct();
	}

	/**
	 * 删除重复项
	 * @param bool $unique
	 * @return $this
	 */
	public function unique( bool $unique = true ):self {
		$this->unique = $unique;
		//
		return $this;
	}


	/**
	 * 排序为升序
	 * @param int|null $sort
	 * @return $this
	 */
	public function sort( ?int $sort ):self {
		if( $sort === null ){
			$this->sort = null;
		}else{
			$this->sort = $sort;
		}
		//
		return $this;
	}

	/**
	 * 排序为升序
	 * @return $this
	 */
	public function sortOfAsc(  ):self {
		$this->sort( 1 );
		//
		return $this;
	}

	/**
	 * 类型转换
	 * @param string $setType
	 * @return $this
	 */
	public function setType( string $setType = null ):self {
		$this->setType = $setType;
		//
		return $this;
	}

	/**
	 * 类型转换为数字
	 * @return $this
	 */
	public function setTypeOfInteger(  ):self {
		$this->setType( com\Lib\Var_\DataType::$integer );
		//
		return $this;
	}

	/**
	 * @return nameUtils\StringHelper
	 */
	public function toStringHelper(  ):nameUtils\StringHelper {
		return nameUtils\StringHelper::new( $this->toArray() );
	}

	/**
	 * @return com\Lib\Hyperf\Model\Collection
	 */
	public function toCollection(  ):com\Lib\Hyperf\Model\Collection {
		return com\Lib\Hyperf\Model\Collection::make( $this->toArray() );
	}

	/**
	 * 获取数组
	 * @return array
	 */
	public function toArray(  ):array {
		$this->data = com\Lib\Fun\Array_::toArray( $this->data, [
			'unique' => $this->unique,
			'sort' => $this->sort,
			'setType' => $this->setType,
		]);
		//
		return $this->data;
	}

	/**
	 * 构建keyname为指定路径值的索引数组
	 * @return array
	 */
	public function toIndexArray():array {
		//
		return com\Lib\Fun\Array_::buildIndexArrayOfKeyPath( $this->toArray(), '' );
	}

	/**
	 * 反转数组中的键名和对应关联的键值
	 * @param array $param 参数数组 <br />
	 * ················· [可空](str)	$param['keyPath']	说明：str  数组路径（支持格式：1.PHP数组访问的格式，如：['a'][0]['b']， 2."->"分割，如：a->b->c）。[默认]使用系统内置函数处理(array_flip)	<br />
	 * ················· [可空](int)	$param['mergeConflict']	说明：是否合并反转后相同键名的数组。[0][默认]否 [1]是	<br />
	 * ················· [可空](mixed)	$param['newValue']	说明：反转后的新键值	<br />
	 * ················· [可空/多选_A/必须](unknown_type)	$param['AAAAA']	说明：AAAAA	<br />
	 * @return array
	 */
	public function arrayFlip( array $param = [] ):array {
		//
		return com\Lib\Fun\Array_::arrayFlip( $this->toArray(), $param );
	}

	// ===============================
	//
	//
	// 下面为静态方法
	//
	//
	// ===============================

	/**
	 * @param array $data
	 * @return $this
	 */
	public static function makeOfNew( array $data ):self {
		return self::make( ...func_get_args() );
	}

	/**
	 * @param int|string|array $data 要分隔的字符(支持字符串(1,2,3)、数组[1,2,3]、纯数字[1])
	 * @return nameArrayHelper\Trim
	 */
	public static function trim( $data ):nameArrayHelper\Trim {
		return nameArrayHelper\Trim::new( $data );
	}

}
